﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefineForm.dao.supplierform;
using UserDefineForm.dao.orm;

namespace UserDefineForm
{
    public partial class OpenSupplierForm : Form
    {
        private List<int> listId;
        private List<String> listName;
        private SupplierFormService formService;
        private SupplierFormDAO formDAO;

        public OpenSupplierForm(SupplierFormService formService)
        {
            InitializeComponent();
            this.formService= formService;
            publishDataToGrid();
        }

        private void initGrid()
        {
            

        }

        private void publishDataToGrid()
        {
            formDAO = SystemFactory.getFormDAO();
            listId = formDAO.getAllFormIdAndName(out listName);
            formNameGrid.Rows.Clear();
            for (int i = 0; i < listName.Count; i++)
            {
                formNameGrid.Rows.Add(new string[] { "" + (i + 1), listName[i] });
            }
            formNameGrid.Refresh();
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            int index = formNameGrid.SelectedRows[0].Index;
            if (index < 0) return;
            int id = listId[index];
            formService.OpenForm(id);
            this.Dispose();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            int index = formNameGrid.SelectedRows[0].Index;
            if (index < 0) return;
            int id = listId[index];
            SupplierFormORM currentForm = SystemFactory.getCurrentForm();
            if (currentForm != null && currentForm.Id == id)
            {
                MessageBox.Show("This form is currently in use !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            formService.DeleteForm(id);
            publishDataToGrid();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
